package com.wz.utils.batch.service.impl;

import com.wz.utils.batch.annotation.BatchDealWithAnt;
import com.wz.utils.batch.entity.User;
import com.wz.utils.batch.mapper.AccountInfoDao;
import com.wz.utils.batch.mapper.ProductInfoDao;
import com.wz.utils.batch.mapper.UserDao;
import com.wz.utils.batch.service.PayService;
import com.wz.utils.batch.service.ProductService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Service
@Transactional(rollbackFor = Exception.class)
public class PayServiceImpl implements PayService {

    @Autowired
    private AccountInfoDao accountInfoDao;

    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserDao userDao;

    @Transactional(propagation = Propagation.REQUIRED)
    public void pay(String accountId, double money) {

        //更新余额
        int retVal = accountInfoDao.updateAccountBlance(accountId,money);


        ((PayService) AopContext.currentProxy()).updateProductStore(1);
       //System.out.println(1/0);
        //嵌套事务
        productService.getProductValue();



    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void updateProductStore(Integer productId){
        try{
            productInfoDao.updatePrice(1,"3.88899999");
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @Override
    @BatchDealWithAnt
    public void batchInsert(List<User> userList) {
        userDao.batchUpdate(userList);
    }
}